1623B - Game on Ranges - CodeForces Solution


brute force dfs and similar implementation sortings *1100

Please click on ads to support us..

Python Code:

T = int(input())
for _ in range(T):
    n = int(input())
    ranges = []
    for i in range(n):
        x, y = map(int, input().split())
        ranges.append([x,y])
    ranges.sort()
    marker = []
    i = 0
    while i < n:
        if len(marker) == 0:
            marker.append(ranges[i])
        elif ranges[i][0] == marker[-1][0]:
            marker = [ranges[i]] + marker
        else:
            for j in range(len(marker)-1):
                if marker[j][1] - marker[j+1][1] == 1:
                    print(str(marker[j][0]) + ' ' + str(marker[j][1]) + ' ' + str(marker[j][1]))
                else:
                    print(str(marker[j][0]) + ' ' + str(marker[j][1]) + ' ' + str(marker[j+1][1]+1))
            if marker[-1][1] == marker[-1][0]:
                print(str(marker[-1][0]) + ' ' + str(marker[-1][1]) + ' ' + str(marker[-1][1]))
            else:
                if ranges[i][0] - marker[0][0] == 1:
                    print(str(marker[-1][0]) + ' ' + str(marker[-1][1]) + ' ' + str(marker[-1][0]))
                else:
                    print(str(marker[-1][0]) + ' ' + str(marker[-1][1]) + ' ' + str(ranges[i][0]-1))
            marker = [ranges[i]]
        i += 1
    for j in range(len(marker) - 1):
        if marker[j][1] - marker[j + 1][1] == 1:
            print(str(marker[j][0]) + ' ' + str(marker[j][1]) + ' ' + str(marker[j][1]))
        else:
            print(str(marker[j][0]) + ' ' + str(marker[j][1]) + ' ' + str(marker[j + 1][1] + 1))
    print(str(marker[-1][0]) + ' ' + str(marker[-1][1]) + ' ' + str(marker[-1][1]))

C++ Code:

#include<bits/stdc++.h>
using namespace std;
int main()
{
int t;
cin>>t;
while(t--)
{
int n;
cin>>n;
int l[n],r[n];
for(int i=0;i<n;i++)
cin>>l[i]>>r[i];
for(int i=0;i<n;i++)
{
int ans=l[i];
for(int j=0;j<n;j++)
if(l[j]==l[i]&&r[j]<r[i])
ans=max(ans,r[j]+1);
cout<<l[i]<<" "<<r[i]<<" "<<ans<<"\n";
}
}
}


Comments

Submit
0 Comments
More Questions

1609A - Divide and Multiply
149B - Martian Clock
205A - Little Elephant and Rozdil
1609B - William the Vigilant
978B - File Name
1426B - Symmetric Matrix
732B - Cormen --- The Best Friend Of a Man
1369A - FashionabLee
1474B - Different Divisors
1632B - Roof Construction
388A - Fox and Box Accumulation
451A - Game With Sticks
768A - Oath of the Night's Watch
156C - Cipher
545D - Queue
459B - Pashmak and Flowers
1538A - Stone Game
1454C - Sequence Transformation
165B - Burning Midnight Oil
17A - Noldbach problem
1350A - Orac and Factors
1373A - Donut Shops
26A - Almost Prime
1656E - Equal Tree Sums
1656B - Subtract Operation
1656A - Good Pairs
1367A - Short Substrings
87A - Trains
664A - Complicated GCD
1635D - Infinite Set